<?php
set_time_limit(0);

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

$this->LoadResurces('mysql/class.mysql');
$db_mysql = new db_mysql();
$db_mysql->connect(\config\ConfigLocal::getLocalConfog()->db['base']['host'], \config\ConfigLocal::getLocalConfog()->db['base']['user'], \config\ConfigLocal::getLocalConfog()->db['base']['password'], \config\ConfigLocal::getLocalConfog()->db['base']['dbname']);
mysql_query("SET NAMES '".\config\ConfigLocal::getLocalConfog()->db['base']['charset']."'");

if(empty($this->Param['option'])){
    $wg = $db_mysql->fetch_array_all('SELECT * FROM `sl_workgroup` WHERE `wg_type` = "业务部门" AND `wg_status` = "ok"');
    $this->Assign('wg', $wg);
    echo $this->GetView('index.php');
}else{
    switch($this->Param['option']){
        case 'save':
            $wg_id = $this->Param['wg_id'];
            $where = 'o_status != "作废"';
            if(!empty($wg_id)){
                $where .= ' AND `o_branch_id` = '.$wg_id;
            }
            $count = $db_mysql->fetch_first('SELECT COUNT(*) AS num FROM `sl_order` WHERE '.$where);
            $count = empty($count) ? 0 : $count['num'];
            for($x = 1; $x <= ceil($count/100); $x++){
                $data = $db_mysql->fetch_array_all('SELECT * FROM `sl_order` WHERE '.$where.' LIMIT '.(($x-1) * 100).',100');
                if(!empty($data)) foreach ($data as $k => $v){
                    $wg_row = $db_mysql->fetch_first('SELECT wg_bh FROM `sl_workgroup` WHERE wg_id = '.$v['o_branch_id']);
                    $check_order = $OrderHelper->orderRow(array('`o_number` = ?' => $v['o_number']));
                    if(!empty($check_order)){
                        continue;
                    }
                    $branch_row = $BranchHelper->branchRow(array('`br_code` = ?' => $wg_row['wg_bh'], '`br_status` = ?' => 'ok'));
                    $bank_row = $BankHelper->bankRow(array('`bk_name` = ?' => $v['o_back'], '`bk_status` = ?' => 'ok'));
                    
                    $data = array(
                        'o_number' => $v['o_number'],
                        'o_vip_name' => $v['o_vip_name'],
                        'o_vip_card' => $v['o_vip_card'],
                        'o_vip_tel' => $v['o_vip_tel'],
                        'o_uname' => $v['o_uname'],
                        'o_branch' => $v['o_branch'],
                        'o_branch_type' => 'dot',
                        'o_branch_id' => empty($branch_row) ? 0 : $branch_row['br_id'],
                        'o_bank' => $v['o_back'],
                        'o_bank_id' => empty($bank_row) ? 0 : $bank_row['bk_id'],
                        'o_add_time' => $v['o_sell_time'],
                        'o_sell_time' => $v['o_sell_time'],
                        'o_search_time' => $v['o_search_time'],
                        'o_finishs_time' => $v['o_finishs_time'],
                        'o_fuser_time' => $v['o_fuser_time'],
                        'o_finishe_time' => $v['o_finishe_time'],
                        'o_status' => $v['o_status'],
                        'o_before_status' => $v['o_before_status'],
                        'o_quit_status' => $v['o_quit_status'],
                        'o_first_time' => $v['o_first_time'],
                        'o_first_atime' => $v['o_first_atime'],
                        'o_visa_time' => $v['o_visa_time'],
                        'o_web_risk' => $v['o_web_risk'],
                        'o_rebate_per' => $v['o_rebate_per'],
                        'o_oldsys' => 1
                    );
                    $oid = $OrderHelper->orderdtSave($data);
                    if($oid){
                        echo '<p>'.$v['o_number'].'转换成功</p>';
                    }else{
                        echo '<p style="color:#ff0000">'.$v['o_number'].'转换失败</p>';
                    }
                }
                unset($data);
            }
            echo '<p>处理完毕</p>';
            break;
    }
}